#include "messageeditarea.h"

MessageEditArea::MessageEditArea(QWidget *parent)
    : QWidget{parent}
{
    // 初始化必要属性
    this->setFixedHeight(200);
    this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    QVBoxLayout* vlayout = new QVBoxLayout();
    vlayout->setSpacing(0);
    vlayout->setContentsMargins(0, 0, 10, 10);
    this->setLayout(vlayout);

    QHBoxLayout* hlayout = new QHBoxLayout();
    hlayout->setSpacing(5);
    hlayout->setContentsMargins(5, 5, 0, 0);
    hlayout->setAlignment(Qt::AlignLeft | Qt::AlignTop);

    vlayout->addLayout(hlayout);

    // 四个按钮
    QString style = "QPushButton { border:none; }";
    style += "QPushButton:pressed { background-color:rgb(255, 255, 255); }";
    style += "QPushButton:hover { background-color:rgb(200, 200, 200); }";

    sendImageBtn = new QPushButton();
    sendImageBtn->setFixedSize(QSize(20, 20));
    sendImageBtn->setIcon(QIcon(":/resource/image/image.png"));
    sendImageBtn->setIconSize(QSize(20, 20));
    sendImageBtn->setStyleSheet(style);

    sendFileBtn = new QPushButton();
    sendFileBtn->setFixedSize(QSize(20, 20));
    sendFileBtn->setIcon(QIcon(":/resource/image/file.png"));
    sendFileBtn->setIconSize(QSize(20, 20));
    sendFileBtn->setStyleSheet(style);

    sendSpeechBtn = new QPushButton();
    sendSpeechBtn->setFixedSize(QSize(20, 20));
    sendSpeechBtn->setIcon(QIcon(":/resource/image/speech.png"));
    sendSpeechBtn->setIconSize(QSize(20, 20));
    sendSpeechBtn->setStyleSheet(style);

    historyBtn = new QPushButton();
    historyBtn->setFixedSize(QSize(20, 20));
    historyBtn->setIcon(QIcon(":/resource/image/history.png"));
    historyBtn->setIconSize(QSize(20, 20));
    historyBtn->setStyleSheet(style);

    hlayout->addWidget(sendImageBtn);
    hlayout->addWidget(sendFileBtn);
    hlayout->addWidget(sendSpeechBtn);
    hlayout->addWidget(historyBtn);

    // 文本编辑区域
    textEdit = new QPlainTextEdit();
    QString s1 = "QScrollBar:vertical { width:1px; }";
    s1 += "QPlainTextEdit { font-size:20px; border:none; color:rgb(1, 1, 1); padding:5px; }";
    textEdit->setStyleSheet(s1);
    vlayout->addWidget(textEdit);

    // 发送按钮
    sendBtn = new QPushButton("发送");
    QString s = "QPushButton { font-size:20px; color:rgb(180, 180, 180); }";
    s += "QPushButton:hover { background-color:rgb(200, 200, 200); }";
    s += "QPushButton:pressed { background-color:rgb(230, 230, 230); }";
    sendBtn->setStyleSheet(s);
    vlayout->addWidget(sendBtn, 0, Qt::AlignRight | Qt::AlignBottom);

    // initialize signal and slot
    initSignalSlot();
}

QPushButton *MessageEditArea::getHistoryBtn()
{
    return historyBtn;
}

void MessageEditArea::initSignalSlot()
{
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    // click sendBtn, send msg
    connect(sendBtn, &QPushButton::clicked, this, &MessageEditArea::sendMessage);

    connect(dataCenter, &model::DataCenter::sendMessageDone, this, &MessageEditArea::addSelfMessage, Qt::UniqueConnection);
    connect(dataCenter, &model::DataCenter::receiveMessageDone, this, &MessageEditArea::addOtherMessage, Qt::UniqueConnection);
}

void MessageEditArea::sendMessage()
{
    // 1.if no session is selected, send failed
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    if(dataCenter->getCurrentSessionId() == ""){
        Toast::makeToast("no session is selected");
        return;
    }
    // 2.fetch message
    QString text = textEdit->toPlainText().trimmed();
    if(text.isEmpty()) {
        return;
    }
    // 3.clear the plainText
    textEdit->clear();
    // 4.send msg to server
    dataCenter->sendTextMessageAsync(dataCenter->getCurrentSessionId(), text);
}

void MessageEditArea::addSelfMessage(model::MessageType type, const QByteArray& content, const QString& extraInfo)
{
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    QString currentSessionId = dataCenter->getCurrentSessionId();

    model::Message msg = model::Message::makeMessage(type, currentSessionId, *dataCenter->getMyself(), content, extraInfo);
    dataCenter->addMessage(msg);

    MainWidget* mainWidget = MainWidget::getInstance();
    MessageShowArea* area = mainWidget->getMessageShowArea();
    area->addMsgItem(false, msg);

    area->verticalBarToEnd();

    // update the last message of session
    emit dataCenter->addSelfMessageDone(currentSessionId);
}

void MessageEditArea::addOtherMessage(const Message &lastMsg)
{
    MainWidget* mainWidget = MainWidget::getInstance();
    MessageShowArea* area = mainWidget->getMessageShowArea();
    area->addMsgItem(true, lastMsg);

    area->verticalBarToEnd();
}
